package com.link.base.base.fund.controller;


import com.link.base.user.model.CoreUser;
import com.link.base.base.accnt.model.AcctAddress;
import com.link.base.base.fund.model.DealerAccount;
import com.link.base.base.fund.service.DealerAccountService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/12 10:05
 * @version 1.0
 */

@Controller
@RequestMapping("/link/dealerAccount")
public class DealerAccountController extends BasicController<DealerAccount> {

    @Resource
    private DealerAccountService dealerAccountService;

    @Override
    public BasicService<DealerAccount> getBasicService() throws Exception {
        return dealerAccountService;
    }

    @RequestMapping(value = "/queryDmsDealarAccount")
    @ResponseBody
    public Map<String, Object> queryDmsDealarAccount(@JsonParam QueryParams qps,@JsonParam DealerAccount entity, HttpSession session,
                                                     HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        CoreUser user = UserUtil.getUser();
        if (null == user.getAcctId()) {
            result.put("success", false);
            result.put("result", "无法获取当前用户经销商信息");
            return result;
        }
        qps.addFilter(new Filter("headId0123213131", "accntId", Filter.OP_EQUAL, String.valueOf(user.getAcctId())));
        return this.queryByExamplePage(qps, entity, session, request, response);
    }

    @RequestMapping(value = "/listByAcctId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> listByAcctId(@JsonParam QueryParams qps, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<DealerAccount> list = null;
        DealerAccount entity = new DealerAccount();
        try {
            qps.invokePreFilterAndSorter();
            qps.preDealSecurity(request);
            if (qps.getAttr1() != null && !"".equals(qps.getAttr1())) {
                qps.addFilter(new Filter("accntId", qps.getAttr1()));
            } else {
                throw new ServiceException("PUBLIC-026");
            }
            DealerAccount t = (DealerAccount) BasicModel.transformClass(entity.getClass(), qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            list = dealerAccountService.queryByExamplePage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<AcctAddress>());
            result.put("result", e.getMessage());
        }
        return result;
    }
}
